import Vue from "vue";
import VueRouter from "vue-router";
import Home from "../views/Home.vue";
import store from "@/store";
//解决重复路由报错
// 把这段代码直接粘贴到router/index.js中的Vue.use(VueRouter)之前
const originalPush = VueRouter.prototype.push;
VueRouter.prototype.push = function (location) {
  return originalPush.call(this, location).catch((err) => {});
};
Vue.use(VueRouter);

const routes = [
  {
    path: "/",
    redirect: "/home",
  },
  {
    path: "/home",
    name: "home",
    component: Home,
  },
  {
    path: "/user",
    name: "user",
    component: () => import(/* webpackChunkName: "user" */ "../views/User.vue"),
    redirect:"/user/cart",
    children: [
      {
        path: "cart",
        component: () =>
          import(
            /* webpackChunkName: "user/cart" */ "../components/user/Cart.vue"
          ),
      },
    ],
    //报错提示，在上面路由都没有的情况下，进入该路由
    
    //独享路由卫士 只针对这里路由有效
    // beforeEnter: (to, from, next) => {
    //   let token = localStorage.getItem("x-auth-token");
    //   if (token) {
    //     next();
    //   } else {
    //     store.dispatch("toastShow/asyncChangeToastShow", {
    //       toastType: "warning",
    //       toastMsg: "你还没有登录",
    //     });
    //   }
    // },
  },
  {
    path: "/goods",
    name: "goods",
    component: () =>
      import(/* webpackChunkName: "goods" */ "../views/Goods.vue"),
  },
  {
    path: "/free",
    name: "free",
    component: () => import(/* webpackChunkName: "free" */ "../views/Free.vue"),
  },
  {
    path: "/order",
    name: "order",
    component: () =>
      import(/* webpackChunkName: "order" */ "../views/Order.vue"),
  },
  {
    path: "/details",
    name: "details",
    component: () =>
      import(/* webpackChunkName: "details" */ "../views/Details.vue"),
  },
  {
    path:"*",
    component:()=>import( /* webpackChunkName: "error" */ "../views/Error.vue")
  }
];

const router = new VueRouter({
  mode: "history",
  base: process.env.BASE_URL,
  routes,
});
//全局前置路由守卫
// router.beforeEach((to, from, next) => {
//   if (to.path == "/user") {
//     let token = localStorage.getItem("x-auth-token");
//     if (token) {
//       next();
//     } else {
//       store.dispatch("toastShow/asyncChangeToastShow", {
//         toastType: "warning",
//         toastMsg: "你还没有登录",
//       });
//     }
//     return;
//   }
//   next();
// });

export default router;
